<?php

namespace app\index\model;

use think\Model;
use think\Db;
//用来处理服务明细定时扣款问题
class Deduction extends Model
{
    //查询服务明细表
    public function getTodayServiceList(){

        $serviceList=Db::name('Service_detail')->whereTime('append_time','d')->select();
        $serviceArray=array();
        $totalMoney=0;
        foreach ($serviceList as $key=>$value){
            //查出学生的学校
           // $totalServiceMoney=0;
            $enrollInfo=$this->getStudentEnrollInfo($value['student_id']);
            if(!empty($enrollInfo)){
                $result=$this->getCampusDetailList($enrollInfo['orgid'],$enrollInfo['shopid'],$value['student_id']);
                if($result){
                    $serviceKey=$enrollInfo['orgid'].','.$enrollInfo['shopid'];
                    $serviceArray[$serviceKey]['childIds'][]=$value['student_id'];
                    $totalMoney+=$value['service_money'];
                    $serviceArray[$serviceKey]['totalMoney']=$totalMoney;
                }else{
                    echo $value['student_id'].'Already buckled';
                }
            }else{
                echo $value['student_id'].'No enrolment';
            }
        }
        $this->addCampusDetail($serviceArray);
    }

    //增加service_campus_detail
    public function addCampusDetail($serviceArray){
//        dump($serviceArray);
//        die;
        foreach($serviceArray as $key=>$value){
            Db::startTrans();
            $keyArray=explode(',',$key);


            try {

               $orderId = $this->addCampusOrder($keyArray[0], $keyArray[1], $value['totalMoney']);
                $data = array();
                $data['effect'] = 1;
                $data['active'] = 1;
                $data['detail_money'] = $value['totalMoney'];
                $data['detail_grant'] = 0;
                $data['detail_type'] = 2;
                $data['pay_time'] = time();
                $data['org_id'] = $keyArray[0];
                $data['shop_id'] = $keyArray[1];
                $data['pay_type'] = 1;
                $data['order_id']=$orderId;
                $data['extend_001_field'] = implode(',', $value['childIds']);
                $rs = Db::name('Campus_detail')->insertGetId($data);
                $result=$this->updateBalance($keyArray[0],$keyArray[1],$value['totalMoney']);
                Db::commit();
            }catch(\Exception $e){
                Db::rollback();
                echo 'System problems';
            }
        }
    }

    //增加campus_order 记录
    public function addCampusOrder($orgId,$shopId,$orderMoney){
        $data=array();
        $data['effect']=1;
        $data['active']=1;
        $data['order_number']=$shopId.'_'.time();
        $data['order_time']=time();
        $data['org_id']=$orgId;
        $data['shop_id']=$shopId;
        $data['order_money']=$orderMoney;
        $data['order_grant']=0;
        $data['order_status']=1;
        $data['order_indate']=time();
        dump($data);

        $rs=Db::name('Campus_order')->insertGetId($data);
        if(empty($rs)){
            echo 'Unable to add order table data';
        }
        return $rs;

    }

    //更新campus_balance记录
    public function updateBalance($orgId,$schoolId,$charge){
        $map['org_id']=$orgId;
        $map['shop_id']=$schoolId;
        $map['effect']=1;
        $map['active']=1;
        $balanceInfo=Db::name('Campus_balance')->where($map)->find();
        if(!empty($balanceInfo)){
            $rs=Db::name('Campus_balance')->where($map)->setDec('charge',$charge);
        }else{
            echo 'balanceInfo exception';
        }
        return $rs;

    }
    //获取学生报名的机构信息
    public function getStudentEnrollInfo($studentId){
        $map['studentid']=$studentId;
        $enrollInfo=Db::name('Childcare_enroll')->where($map)->find();
        return $enrollInfo;
    }

    //查出htg_campus_detail表的记录判断是否已经扣款
    public function getCampusDetailList($orgId,$schoolId,$studentId){
        $map['org_id']=$orgId;
        $map['shop_id']=$schoolId;
        $map['detail_type']=2;
        $detailList=Db::name('Campus_detail')->where($map)->whereTime('pay_time','d')->select();
        if(!empty($detailList)){
            foreach($detailList as $key=>$value){
                $childIds=explode(',',$value['extend_001_field']);
                if(in_array($studentId,$childIds)){
                    return false;
                }
            }
        }
        return true;
    }
}
